﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Reflection;
using System.IO;
using System.Diagnostics;
using emulib;

namespace mmoemu
{
    class Program
    {
        static void Main(string[] args)
        {
            Char dirSeperator = Path.DirectorySeparatorChar;
            if (args.Length == 1 && args[0].Equals("-help"))
            {
                Console.WriteLine("-help" + Environment.NewLine + "\tshow this help");
                Console.WriteLine("-clist ServerLib.dll" + Environment.NewLine + "\tshow all of the public classes in ServerLib.dll" + Environment.NewLine + "\t(if you don't know namespace.ClassName)");
                Console.WriteLine("-run ServerLib.dll" + Environment.NewLine + "\tnamespace.Classname run the specified server class" + Environment.NewLine + "\tnamespace.Classname in ServerLib.dll");
                Console.WriteLine();
                Console.WriteLine("Note: All server dll's MUST be in the ." + dirSeperator + "servers directory");
                Environment.Exit(0);
            } 
            if (!Directory.Exists("." + dirSeperator + "plugins"))
            {
                Directory.CreateDirectory("." + dirSeperator + "plugins");
            }
            if (!Directory.Exists("." + dirSeperator + "servers"))
            {
                Directory.CreateDirectory("." + dirSeperator + "servers");
            }
            if (args.Length > 1)
            {
                String fullpath = Path.GetFullPath("." + dirSeperator + "servers");
                fullpath += dirSeperator + args[1];
                Debug.WriteLine(fullpath);
                try
                {
                    if (!File.Exists(fullpath))
                    {
                        throw new Exception("Server DLL not found!");
                    }
                    //visual studio seems to think this is deprecated...
                    //msdn disagrees :<
                    Assembly asm = Assembly.LoadFile(fullpath);
                    if (args[0].Equals("-run"))
                    {
                        Type asmClassName = asm.GetType(args[2]);
                        IMmoServ ims = Activator.CreateInstance(asmClassName) as IMmoServ;
                        if (args.Length > 3)
                        {
                            ims.Run(args[3]);
                        }
                        else
                        {
                            ims.Run();
                        }
                        //sit around with the console open until told to stop
                        //this depends on the implementing server to handle commands
                        String cr;
                        Console.Write("#");
                        while ((cr = Console.ReadLine()) != "stop")
                        {
                            if (cr != String.Empty)
                            {
                                //blank line is to make commands easier to read back
                                Console.WriteLine();
                                ims.HandleCmd(cr.Split(new Char[] { ' ' }));
                            }
                            Console.WriteLine();
                            Console.Write("#");
                        }
                        ims.Stop();
                    }
                    else if (args[0].Equals("-clist"))
                    {
                        Console.WriteLine("Listing classes in " + fullpath);
                        foreach (Type t in asm.GetTypes())
                        {
                            Console.WriteLine(t.FullName);
                        }
                    }
                    else
                    {
                        Console.WriteLine("Unknown command" + args[0]);
                    }
                    Console.WriteLine();
                    Console.Write("Press any key to exit.");
                    Console.ReadKey(true);
                    Environment.Exit(0);
                }
                catch (Exception e)
                {                    
                    Console.WriteLine(e.Message);
                    Console.WriteLine("Unable to load an IMmoServ interface in:");
                    Console.WriteLine(fullpath);
                    Console.WriteLine("Did you forget to include the file extension?");
                    Console.WriteLine("Are you sure " + args[1] + " is the correct namespace.ClassName?");
                    Console.WriteLine("Stopping.");
                    Console.ReadKey(true);
                    //eh... 0 probably isn't best but w/e
                    Environment.Exit(0);
                }
            }
            Console.WriteLine("I couldn't figure out what to do! Check your arguments!");
            Console.WriteLine("Run mmoemu -help if you don't know what to do!");
        }
    }
}
